package com.fzb.ftp;

import javax.security.auth.login.LoginException;

public class FtpResponseImpl implements IFtpResponse {

	private FtpSession ftpSession;
	
	public FtpResponseImpl(FtpSession ftpSession) {
		this.ftpSession=ftpSession;
	}
	
	@Override
	public void connectSucc(String str) {
		ftpSession.sendMsg(new FtpReqeustImpl(ftpSession).inputUserName());
	}

	@Override
	public void inputUserName(String str) {
		ftpSession.sendMsg(new FtpReqeustImpl(ftpSession).inputUserName());
	}

	@Override
	public void inputPassword(String str) {
		ftpSession.sendMsg(new FtpReqeustImpl(ftpSession).inputPassword());
	}

	@Override
	public void loginSucc(String str) {
		System.out.println("login succ");
		ftpSession.sendMsg(new FtpReqeustImpl(ftpSession).changeToSYST());
		if(ftpSession.getAttr().get("callBack")!=null){
			((FtpLoginSuccCallBack)(ftpSession.getAttr().get("callBack"))).callBack(ftpSession);
		}
		
	}

	@Override
	public void cmdSucc(String str) {
		
	}

	@Override
	public void initCmdSucc(String str) {
		new FtpReqeustImpl(ftpSession).sendCmd();
	}

	@Override
	public void modelChanageSucc(String str) {
		
	}

	@Override
	public void passiveModeSucc(String str) {
		String addr = str.substring(str.indexOf("(") + 1, str.lastIndexOf(")"));
		ftpSession.getAttr().put("port", Integer.parseInt(addr.split(",")[4]) * 256
				+ Integer.parseInt(addr.split(",")[5]));
	}

	@Override
	public void tipsMsg(String str) {
		new FtpReqeustImpl(ftpSession).sendCmd();
	}

	@Override
	public void loginFailed(String str) throws LoginException {
		throw new LoginException("userName-> "+ftpSession.getFtpUser().getUserName() +" login failed");
	}
}
